package com.zhaoc.service.impl;

import com.zhaoc.dao.CourierRepository;
import com.zhaoc.dao.FixAreaRepository;
import com.zhaoc.dao.TakeTimeRepository;
import com.zhaoc.domain.Courier;
import com.zhaoc.domain.FixedArea;
import com.zhaoc.domain.TakeTime;
import com.zhaoc.service.IFixAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>title:com.zhaoc.service.impl</p>
 * date:2018/8/3 0003
 * time:11:49
 * author:赵成
 * user:13566
 */
@Service
@Transactional
public class FixAreaServiceImpl implements IFixAreaService {

    @Autowired
    private FixAreaRepository fixAreaRepository;

    //保存定区
    @Override
    public void save(FixedArea model) {
        fixAreaRepository.save(model);
    }

    //按条件分页查询
    @Override
    public Page<FixedArea> findPageDate(Specification<FixedArea> specification, Pageable pageable) {
        return fixAreaRepository.findAll(specification,pageable);
    }


    @Autowired
    private CourierRepository courierRepository;
    @Autowired
    private TakeTimeRepository takeTimeRepository;
    //快递员关联定区
    @Override
    public void associactionCourierTofixedArea(String id, Integer courierId, Integer takeTimeId) {
        //先根据id,分别查定区,快递员,收派时间
        FixedArea noAssociFixedArea = fixAreaRepository.findOne(id);
        Courier noAssociCourier = courierRepository.findOne(courierId);
        TakeTime noAssociTakeTime = takeTimeRepository.findOne(takeTimeId);

        //将快递员关联到定区
        noAssociFixedArea.getCouriers().add(noAssociCourier);

        //将收派标准关联到快递员
        noAssociCourier.setTakeTime(noAssociTakeTime);
    }
}
